package com.sduept.nwld.wave.manager;

import com.sduept.cim.model.protection.WaveEquipment;
import com.sduept.core.dao.SuperDAO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Slf4j
@Service
public class SFWaveEquipmentMatchManager {
	
	@Autowired
	private SuperDAO dao;
	
	
	public void matchEquipment(){
		//List<WaveEquipment> waves = dao.findAll(WaveEquipment.class);
		List<WaveEquipment> waves =dao.findByCondition(WaveEquipment.class,"name is not null and "
				+ "sub_id is not null and  privateStationId is not null") ;
		String sql="select name,sub_id,privateStationId,id from t_cim_waverecord_equipment_back where name is not null and "
				+ "sub_id is not null and  privateStationId is not null ";
		List<?> xjWaves = dao.nativeQuery(sql);
		List<String> msgs = new ArrayList<String>();
		List<String> noMatchMsg = new ArrayList<String>();
		for(WaveEquipment wave:waves){
			boolean notfind = true;
			for(Object obj:xjWaves){
				Object[] objs=(Object[])obj;
				if(!StringUtils.isEmpty(wave.getName())&&objs[0]!=null&&!StringUtils.isEmpty(objs[0].toString())){
					String name=objs[0].toString();
					String subId=objs[1]==null?null:objs[1].toString();
					String privateStationId=objs[2]==null?null:objs[2].toString();
					if(StringUtils.isNotEmpty(subId)&&StringUtils.isNotEmpty(privateStationId)){
						if(StringUtils.isNotEmpty(wave.getName())&&StringUtils.isNotEmpty(wave.getSubId())&&name.contains(wave.getName())&&subId.contains(wave.getSubId())){
							msgs.add(wave.getId()+":"+privateStationId+"\\录波器\\"+name);
							log.info(wave.getId()+":"+privateStationId+"\\录波器\\"+name);
							notfind = false;
							break;
						}
					}
				}
			}
			if(notfind){
				for(Object obj:xjWaves){
					Object[] objs=(Object[])obj;
					if(!StringUtils.isEmpty(wave.getName())&&objs[0]!=null&&!StringUtils.isEmpty(objs[0].toString())){
						String name=objs[0].toString();
						String subId=objs[1]==null?null:objs[1].toString();
						String privateStationId=objs[2]==null?null:objs[2].toString();
						if(StringUtils.isNotEmpty(subId)&&StringUtils.isNotEmpty(privateStationId)){
							if(StringUtils.isNotEmpty(wave.getName())&&StringUtils.isNotEmpty(wave.getSubId())&&subId.contains(wave.getSubId())){
								if(name.contains("(")){
									name=name.substring(0, name.lastIndexOf("("));
								}
								boolean isContain=false;
								if(name.length()>wave.getName().length()){
									isContain=isContain(name,wave.getName());
								}else{
									isContain=isContain(wave.getName(),name);
								}
								if(name.contains(wave.getName())||wave.getName().contains(name)||isContain){
									msgs.add(wave.getId()+":"+privateStationId+"\\录波器\\"+name);
									log.info(wave.getId()+":"+privateStationId+"\\录波器\\"+name);
									notfind = false;
									break;
								}
							}
						}
					}
				}
			}
//			if(notfind){
//				for(Object obj:xjWaves){
//					Object[] objs=(Object[])obj;
//					if(!StringUtils.isEmpty(wave.getName())&&objs[0]!=null&&!StringUtils.isEmpty(objs[0].toString())){
//						String name=objs[0].toString();
//						String subId=objs[1]==null?null:objs[1].toString();
//						String privateStationId=objs[2]==null?null:objs[2].toString();
//						if(StringUtils.isNotEmpty(subId)&&StringUtils.isNotEmpty(privateStationId)){
//							if(StringUtils.isNotEmpty(wave.getName())&&StringUtils.isNotEmpty(wave.getSubId())&&subId.contains(wave.getSubId())){
//								if(StringUtils.isNotEmpty(wave.getDevType())&&wave.getName().contains(wave.getDevType())){
//									String s1=wave.getName().substring(0,wave.getName().indexOf(wave.getDevType()));
//									boolean isContain=false;
//									if(name.length()>s1.length()){
//										isContain=isContain(name,s1);
//									}else{
//										isContain=isContain(s1,name);
//									}
//									if(name.contains(wave.getName())||wave.getName().contains(name)||isContain){
//										msgs.add(wave.getId()+":"+privateStationId+"\\录波器\\"+name);
//										log.info(wave.getId()+":"+privateStationId+"\\录波器\\"+name);
//										notfind = false;
//										break;
//									}
//								}
//								
//							}
//						}
//					}
//				}
//			}
			if(notfind){
				//log.info(wave.getSubstationName()+" "+ wave.getName()+" is not matcher");
				noMatchMsg.add(wave.getSubName()+" "+ wave.getName());
			}
		}
		log.info("四方总:"+waves.size()+"许继总:"+xjWaves.size()+"成功匹配:"+msgs.size());
		log.info(String.valueOf(noMatchMsg.size()));
		StringBuilder sb=new StringBuilder();
		for(String s:noMatchMsg){
			sb.append(s+"\n");
		}
		log.info(sb.toString());
	}
	private  boolean isContain(String s1,String s2){
		char [] stringArr = s2.toCharArray();
		int pos[]=new int[stringArr.length];
		for(int i=0;i<stringArr.length;i++){
			String c = String.valueOf(stringArr[i]);
			if(s1.contains(c)){
				pos[i]=s1.indexOf(c);
				if(i!=0){
					if(pos[i]-pos[i-1]<0){
						return false;
					}
				}
			}else{
				return false;
			}
		}
		return true;
	}
}
